﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Browsers;
using ProgNetComponents.Data.SqlCe;
using DevExpress.XtraGrid.Views.Grid;
using Wydatki.Init;
using DevExpress.XtraEditors;
using ProgNetComponentsDx.Options;

namespace Wydatki.Tools
{
    public partial class FormNoweWydatki : XtraForm
    {
        private SqlCeDataManager manager = new SqlCeDataManager();
        private FormNoweWydatki()
        {
            InitializeComponent();
        }
        Zakupy.DataSets.ZakupyDataSet zakupy;
        Setup.DataSets.ProduktyDataSet produkty;
        Setup.DataSets.SklepyDataSet sklepy;

        public FormNoweWydatki(Zakupy.DataSets.ZakupyDataSet zakupy, 
            Setup.DataSets.ProduktyDataSet produkty,
            Setup.DataSets.SklepyDataSet sklepy) :
            this()
        {
            this.zakupy = zakupy;
            this.produkty = produkty;
            this.sklepy = sklepy;
            gridControl1.DataSource = zakupy;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (gridControl1.FocusedView != null)
            {
                (gridControl1.FocusedView as GridView).CloseEditor();
                (gridControl1.FocusedView as GridView).UpdateCurrentRow();
            }
            if (modeProvider1.ValidateRequiredColumns(ref msg))
            {
                DialogResult dlg = System.Windows.Forms.DialogResult.OK;

                if (!Tools.WydatkiTools.ProcessNewProducts(zakupy, produkty, sklepy))
                {
                    dlg = System.Windows.Forms.DialogResult.No;
                }
                else
                {
                    if (!manager.UpdateDataSet(zakupy))
                    {
                        if (manager.LastException != null)
                            manager.LastException.Log();
                        dlg = System.Windows.Forms.DialogResult.No;
                    }
                }

                DialogResult = dlg;
                Close();
            }
            else
            {
                this.ShowDxErrorDialog(msg);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DataRow r = (sender as GridView).GetFocusedDataRow();
                if (r != null)
                    r.Delete();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            manager.AddTableDef(produkty.STP_PRODUKTY);
            manager.AddTableDef(produkty.STP_PROD_KAT);
            manager.AddTableDef(zakupy.WRK_WYDATKI);
            manager.AddTableDef(zakupy.WRK_WYDATKI_PRODUKTY);
            layoutProvider1.ApplySavedLayout();

            if (OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Work.AutoExpandNoweWydatki))
            {
                for (int i=0; i < gridView1.RowCount; i++)
                    gridView1.SetMasterRowExpanded(i, true);
            }
        }
        
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            layoutProvider1.SaveLayout();
        }

        private void rep_WRK_WYD_SKLEP_Settings_CreateNewRow(object sender, CreateNewRowEventArgs e)
        {
            DataRow row = sklepy.STP_SKLEPY.NewRow();
            row["STP_SKL_NAZWA"] = e.DisplayValue;
            sklepy.STP_SKLEPY.Rows.Add(row);
            e.NewRow.Assign(row);
        }

        private void rep_WRK_WYD_PROD_PRODUKT_Settings_CreateNewRow(object sender, CreateNewRowEventArgs e)
        {
            DataRow row = produkty.STP_PRODUKTY.NewRow();
            row["STP_PROD_NAZWA"] = e.DisplayValue;
            produkty.STP_PRODUKTY.Rows.Add(row);
            e.NewRow.Assign(row);
        }

        private void btnResetLayout_Click(object sender, EventArgs e)
        {
            layoutProvider1.RestoreDefaultLayout();
        }

    }
}
